<template>
  <el-drawer
    :visible.sync="drawerVisible"
    title="选择枚举值"
    size="60%"
    direction="rtl"
    :close-on-press-escape="false"
    :wrapperClosable="false"
    :destroy-on-close="true"
    :before-close="beforeClose"
    custom-class="tag-drawer factory-enums-drawer"
    v-drawerResize
  >
    <div class="drawer-wrap">
      <div class="drawer-content tag-scrollbar">
        <tag-content-header search refresh @toolFun="toolFun">
          <template #title>
            <span>枚举值列表</span>
          </template>
        </tag-content-header>
        <tag-table
          ref="table"
          @refresh="getList"
          :data="dataList"
          :loading="loading"
          :columns="columns"
          :params="params"
          :total="totalSize"
          selection
          rowKey="cdVal"
        >
        </tag-table>
      </div>
    </div>
    <div class="drawer-btns">
      <el-button @click="beforeClose()">取消</el-button>
      <el-button type="primary" @click="submit">确定</el-button>
    </div>
  </el-drawer>
</template>

<script>
export default {
  props: {
    tagCode: {
      type: String,
      required: true,
      default: ''
    }
  },
  data() {
    return {
      drawerVisible: false,
      loading: false,
      dataList: [],
      selectedVal: [],
      columns: [
        {
          show: true,
          label: '枚举值',
          prop: 'cdVal',
          search: true,
          searchKey: '',
          align: 'center'
        },
        {
          show: true,
          label: '枚举值描述',
          prop: 'cdDesc',
          search: true,
          searchKey: '',
          align: 'center'
        }
      ],
      params: {
        ascs: [],
        descs: [],
        currentPageNum: 1,
        pageSize: 10,
        queryParams: {},
        descPriority: false
      },
      totalSize: 0
    }
  },
  methods: {
    showDrawer(enums) {
      this.drawerVisible = true
      this.selectedVal = enums
      this.getList()
    },
    getList() {
      this.loading = true
      this.params.queryParams.tagCode = this.tagCode
      this.$ajax({
        url: this.$ajax.setUrl('ast/astTagEnumVal/imageEnumList'),
        method: 'get',
        params: this.$ajax.setParams(this.params)
      }).then(({ data }) => {
        if (data && data.code === '0') {
          this.dataList = data.data.records
          this.totalSize = data.data.totalSize
          this.selectedVal.forEach(v =>
            this.$nextTick(() => {
              let row = this.dataList.find(item => item.cdVal === v)
              if (row) {
                let selectedRow = this.$refs.table.getSelectList()
                if (!selectedRow.find(item => item.cdVal === row.cdVal))
                  this.$refs.table.$refs.tagTable.toggleRowSelection(row)
              }
            })
          )
        } else {
          this.$message.error(data.data || data.msg)
        }
        this.loading = false
      })
    },
    toolFun(operation) {
      switch (operation) {
        case 'search':
          // 搜索动作
          this.$refs['table'].searchToggle()
          break
        case 'refresh':
          // 刷新动作
          this.getList()
          break
      }
    },
    submit() {
      let selectedRow = this.$refs.table.getSelectList()
      let enums = []
      selectedRow.forEach(v => enums.push(v.cdVal))
      this.$emit('callback', enums)
      this.beforeClose()
    },
    beforeClose() {
      this.dataList = []
      this.$refs.table.$refs.tagTable.clearSelection()
      this.drawerVisible = false
    }
  }
}
</script>

<style lang="scss"></style>
